import request from '@/config/axios/appRequest'
import { isEmpty } from 'lodash-es'

export interface productVO {
  spuIds?: EmptyArrayType
  userId?: number
}
export interface orderVO {
  items: any
  couponId: any
  deliveryType: any
  pickUpStoreId: any //自提门店编号
  receiverName: any // 选择门店自提时，该字段为联系人名
  receiverMobile: any // 选择门店自提时，该字段为联系人手机
  pointStatus: any
  combinationActivityId: any
  combinationHeadId: any
  seckillActivityId: any
  pointActivityId: any
  remark?: string //备注
  userId?: number //会员id
  employeeStr?: any //员工信息
  shoppingCartType?: number //订单类型  0 统一 1 单一
  isRepair?: number //是否补录
  repairDate?: any //补录日期
  password?: string //密码
  rebateType?: number //分成类型
  pay?: any //支付项
  no?: any //订单
}
export function getSettlementProduct(params: productVO) {
  return request.get({
    url: '/trade/order/settlement-product',
    params
  })
}
export function settlementOrder(data: orderVO) {
  const data2 = {
    ...data
  }
  // 移除多余字段
  if (!(data.couponId > 0)) {
    delete data2.couponId
  }

  if (!(data.pickUpStoreId > 0)) {
    delete data2.pickUpStoreId
  }
  if (isEmpty(data.receiverName)) {
    delete data2.receiverName
  }
  if (isEmpty(data.receiverMobile)) {
    delete data2.receiverMobile
  }
  if (!(data.combinationActivityId > 0)) {
    delete data2.combinationActivityId
  }
  if (!(data.combinationHeadId > 0)) {
    delete data2.combinationHeadId
  }
  if (!(data.seckillActivityId > 0)) {
    delete data2.seckillActivityId
  }
  if (!(data.pointActivityId > 0)) {
    delete data2.pointActivityId
  }
  // 解决 SpringMVC 接受 List<Item> 参数的问题
  // delete data2.items
  // delete data2.omvoPay
  // delete data2.ppcPay

  // for (let i = 0; i < data.items.length; i++) {
  //   data2[encodeURIComponent('items[' + i + '' + '].skuId')] = data.items[i].skuId + ''
  //   data2[encodeURIComponent('items[' + i + '' + '].count')] = data.items[i].count + ''
  //   if (data.items[i].cartId) {
  //     data2[encodeURIComponent('items[' + i + '' + '].cartId')] = data.items[i].cartId + ''
  //   }
  // }
  // for (let i = 0; i < data.omvoPay.length; i++) {
  //   data2[encodeURIComponent('omvoPay[' + i + '' + '].payStatus')] = data.omvoPay[i].payStatus
  //   data2[encodeURIComponent('omvoPay[' + i + '' + '].paybalance')] =
  //     data.omvoPay[i].paybalance + ''
  // }
  // for (let i = 0; i < data.ppcPay.length; i++) {
  //   data2[encodeURIComponent('ppcPay[' + i + '' + '].payStatus')] = data.ppcPay[i].payStatus
  //   data2[encodeURIComponent('ppcPay[' + i + '' + '].paybalance')] = data.ppcPay[i].paybalance + ''
  //   data2[encodeURIComponent('ppcPay[' + i + '' + '].assetsId')] = data.ppcPay[i].assetsId + ''
  //   data2[encodeURIComponent('ppcPay[' + i + '' + '].usingDis ')] = data.ppcPay[i].usingDis
  // }
  // const queryString = Object.keys(data2)
  //   .map((key) => key + '=' + data2[key])
  //   .join('&')
  return request.get({
    url: `/trade/order/settlement`,
    params: data2
  })
}
export function createOrder(data: orderVO) {
  return request.post({
    url: '/trade/order/create',
    data
  })
}
// 获得订单详细：sync 是可选参数
export function getOrderDetail(id: number, sync?: boolean) {
  return request.get({
    url: '/trade/order/get-detail',
    params: { id, sync }
  })
}
// 取消订单
export function cancelOrder(params) {
  return request.delete({
    url: '/trade/order/cancel',
    params
  })
}
// 预支付-在线支付
export function beforeSubmit(data) {
  return request.post({
    url: '/trade/order/before-submit',
    data
  })
}
// 二次支付
export function secondSettlementOrder(params) {
  return request.get({
    url: `/trade/order/second-settlement`,
    params
  })
}
// 二次在线支付
export function secondSubmit(data) {
  return request.post({
    url: '/trade/order/second-submit',
    data
  })
}
// 创建寄存-提货订单 类型：1 寄存 2 提货
export function createReserveOrder(data: any) {
  return request.post({
    url: '/trade/order/create-reserve',
    data
  })
}
export function adminRefundId(params: any) {
  return request.get({
    url: '/trade/after-sale/admin-refund-id',
    params
  })
}
// 创建还款订单

export function createRepayOrder(data: any) {
  return request.post({
    url: '/trade/order/create-repay',
    data
  })
}
